﻿using System;
using System.IO;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.Drawing.Imaging;
using BrightIdeasSoftware;
using MovieBrowser.Model;

namespace MovieBrowser.Util
{

    class MovieVirtualDataSource : AbstractVirtualListDataSource
    {
        public MovieVirtualDataSource(VirtualObjectListView listView, List<Movie> objectList)
            : base(listView)
        {
            this.Objects = objectList;
        }

        public override int GetObjectIndex(object model)
        {
            return this.Objects.IndexOf((Movie)model);
        }

        public override object GetNthObject(int n)
        {
            Movie p = (Movie)this.Objects[n % this.Objects.Count];
            //p.serialNumber = n;
            return p;
        }

        public override int GetObjectCount()
        {
            return Objects.Count;
        }

        public override void Sort(OLVColumn column, SortOrder order)
        {
            this.Objects.Sort(new MasterListSorter(column, order));
        }

        public override int SearchText(string value, int first, int last, OLVColumn column)
        {
            return DefaultSearchText(value, first, last, column, this);
        }

        List<Movie> Objects;
    }

    internal class MasterListSorter : Comparer<Movie>
    {
        public MasterListSorter(OLVColumn col, SortOrder order)
        {
            this.column = col;
            this.sortOrder = order;
        }
        private OLVColumn column;
        private SortOrder sortOrder;

        public override int Compare(Movie x, Movie y)
        {
            IComparable xValue = this.column.GetValue(x) as IComparable;
            IComparable yValue = this.column.GetValue(y) as IComparable;

            int result = 0;
            if (xValue == null || yValue == null)
            {
                if (xValue == null && yValue == null)
                    result = 0;
                else
                    if (xValue != null)
                        result = 1;
                    else
                        result = -1;
            }
            else
                result = xValue.CompareTo(yValue);

            if (this.sortOrder == SortOrder.Ascending)
                return result;
            else
                return 0 - result;
        }
    }
}
